package com.swh.servic;

import com.swh.dao.MeetingDao;
import com.swh.entity.Meeting;
import com.swh.entity.PageEntity;

import java.util.Date;
import java.util.List;

/**
 * @Author: swh
 * @Description:
 * @Date: 2020/11/25 16:51
 * @Version: 1.0
 */
public class MeetingService {
    private MeetingDao meetingDao = new MeetingDao();

    public PageEntity<Meeting> findPage(Integer status , String title, Integer  currPage ) {

        PageEntity<Meeting> pageEntity = new PageEntity<>();
        // 页大小
        Integer pageSize = pageEntity.getPageSize();

        // 总记录数
        Integer totalCount = meetingDao.countByTitleAndStatus(title, status);

        // 总页数
        Integer totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;

        // 起始索引
        Integer startIndex = (currPage - 1) * pageSize;
        // 当前页的数据
        List<Meeting> meetingList = meetingDao.findPage(title, status, startIndex, pageSize);

        pageEntity.setTotalPage(totalPage);
        pageEntity.setTotalCount(totalCount);
        pageEntity.setData(meetingList);
        pageEntity.setCurrPage(currPage);
        return pageEntity;

    }

    //添加会议
    public void save(Meeting meeting) {
        meeting.setStatus(0L);
        meeting.setPublishDate(new Date());
        meetingDao.save(meeting);
    }

    // 查询会议详情
    public Meeting findById(long id) {
        return meetingDao.findById(id);
    }

    // 实到人数
    public Integer countJoinMeetingByMeetingId(long id) {
        return meetingDao.countJoinMeetingByMeetingId(id);
    }

    // 查询原来是否有参加过这个会议
    public Boolean isJoinMeeting(Long userId, long meetingId) {
        Integer count = meetingDao.countByUserIdAndMeetingId(userId, meetingId);

        return count == 0 ? false : true;
    }

    // 说明原来参加过这个会议
    public void unJoinMeeting(Long userId, Long meetingId) {
        meetingDao.unJoinMeeting(userId, meetingId);
    }

    // 说明未参加过这个会议
    public void joinMeeting(Long userId, Long meetingId) {
        meetingDao.joinMeeting(userId, meetingId);
    }
}
